package com.pubinfo.passbook.common.service.engine.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TEngineComputeUnit;
import com.pubinfo.passbook.common.exception.ServiceException;
import com.pubinfo.passbook.common.mapper.TEngineComputeUnitMapper;
import com.pubinfo.passbook.common.model.dto.engine.EngineUnitParam;
import com.pubinfo.passbook.common.service.engine.EngineComputeUnitService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhouch
 * @date 2021/12/14 14:45
 */
@Service
public class EngineComputeUnitServiceImpl
        extends ServiceImpl<TEngineComputeUnitMapper, TEngineComputeUnit> implements EngineComputeUnitService {
    @Override
    public IPage<TEngineComputeUnit> page(EngineUnitParam param) {
        Page<TEngineComputeUnit> pager = param.getPage();

        return new LambdaQueryChainWrapper<>(baseMapper)
                .like(StringUtils.isNotBlank(param.getKeyword()), TEngineComputeUnit::getName, param.getKeyword())
                .eq(StringUtils.isNotBlank(param.getChainType()), TEngineComputeUnit::getChainType, param.getChainType())
                .page(pager);
    }

    @Override
    public TEngineComputeUnit getBySymbol(String symbol) {
        return new LambdaQueryChainWrapper<>(baseMapper)
                .eq(StringUtils.isNotBlank(symbol), TEngineComputeUnit::getSymbol, symbol)
                .one();
    }


    @Override
    public Boolean existSymbol(String symbol) {

        return new LambdaQueryChainWrapper<>(baseMapper)
                .eq(StringUtils.isNotBlank(symbol), TEngineComputeUnit::getSymbol, symbol)
                .count() > 0;
    }
}
